package com.helen.ui.order;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.AllCommentListAdapter;
import com.helen.entity.AllCommentEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.helen.widget.RecycleViewDivider;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * Created by yang on 2018/5/31.
 * 全部评价
 */

public class AllCommentActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private int page = 1;
    private View errorView;
    private List<AllCommentEntity> list = new ArrayList();
    private AllCommentListAdapter allCommentListAdapter;
    private int iid;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_all_comment;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.all_comment);
        iid = getIntent().getIntExtra("iid",0);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        allCommentListAdapter = new AllCommentListAdapter(R.layout.item_all_comment, list);
        allCommentListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(AllCommentActivity.this));
        rvList.addItemDecoration(new RecycleViewDivider(AllCommentActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_decoration));
        rvList.setAdapter(allCommentListAdapter);
        refresh();

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        allCommentListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
    }


    @Override
    protected void updateViews() {

    }
    private void refresh() {
        commentListApi();
    }


    private void loadMore() {
        page++;
        commentListApi();
    }
    private void commentListApi() {
        HttpParams params = new HttpParams();
        params.put("iid", iid+ "");
        params.put("page", page + "");
        params.put("size", "10");
        MyLog.e("yang", "params==" + params.toString());
        EasyHttp.post(Constants.API_JSONAPI_COMMENTLIST)
                .params(params)
//                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
//                .cacheKey(Constants.API_JSONAPI_ORDER_LIST + "_" + iid)
//                .cacheDiskConverter(new SerializableDiskConverter())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取评论列表失败==" + e.toString());
                        MToast.makeTextShort(AllCommentActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String stringCacheResult) {
                        MyLog.e("yang", "评论列表==" + stringCacheResult);
                        String data = PhoneUtils.checkResponseFlag2(AllCommentActivity.this, stringCacheResult, TAG);
                        if (data != null ) {
                            list = new Gson().fromJson(data, new TypeToken<List<AllCommentEntity>>() {
                            }.getType());
                            if (page == 1) {
                                allCommentListAdapter.setNewData(list);
                            } else {
                                allCommentListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                allCommentListAdapter.loadMoreEnd(false);
                                allCommentListAdapter.setEmptyView(errorView);
                            } else {
                                allCommentListAdapter.loadMoreComplete();
                            }
                        }


                    }
                });
    }

}
